package com.raising.utils;

import com.raising.framework.spring.SpringUtils;
import com.raising.modules.sys.dao.SysTipDao;
import com.raising.modules.sys.entity.SysTipVo;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 字典工具类
 *
 * @author gaoy
 * @version 1.0
 */
public class TipUtils {

    private static SysTipDao sysTipDao = SpringUtils.getBean(SysTipDao.class);

    public static final String CACHE_NAME = "TIP_LIST";

    /**
     * 获取标签
     * @author gaoy
     * @param id                tip标识
     * @param defaultContent    默认提示语
     * @return
     */
    public static String getTip(String id,String defaultContent) {
        List<SysTipVo> tipList = (List<SysTipVo>)JCacheUtils.get(CACHE_NAME);
        //先判断是否有缓存
        if(tipList==null){
            JCacheUtils.put("TIP_LIST",sysTipDao.getList(new SysTipVo()));
            tipList = (List<SysTipVo>)JCacheUtils.get(CACHE_NAME);
        }
        if (StringUtils.isNotBlank(id)){
            for (SysTipVo bean : tipList) {
                if (id.equals(bean.getId())) {
                    if(bean.getContent()==null){
                        return defaultContent;
                    }
                    return bean.getContent();
                }
            }
        }
        return defaultContent;
    }
    /**
     * 刷新tip缓存
     * @author GaoYuan
     * @datetime 2017/10/23 下午7:27
     */
    public static void reloadTip(){
        JCacheUtils.put("TIP_LIST",sysTipDao.getList(new SysTipVo()));
    }
}
